package com.study.notepad.filter;/*
 @Author Cedar
 @DESCRIPTION 
*/

import com.mchange.v2.c3p0.DataSources;
import com.study.notepad.util.DBUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
import java.sql.SQLException;

public class DestroyDataSourceListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        DataSource dataSource = DBUtils.getDataSource();
       /* if ( dataSource instanceof PooledDataSource)
        {
            PooledDataSource pds = (PooledDataSource) dataSource;
            try {
                pds.close();
                System.out.println( "pds closed");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }*/

       try {
           DataSources.destroy(dataSource);
           System.out.println("关闭数据库连接池成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
